package com.whh.zuowen.mapper;

import com.whh.zuowen.pojo.Zuowen;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * UPDATE zuowen
 * SET content = replace( content, '<p style="text-align: center;"><img src="http://filesdown.zuowen.com/img/2019/08/24/082543_5d6084072d166.jpg" title="1566653464237890.jpg" alt="1566653464237890.jpg" width="100" height="100"></p>', '')
 * WHERE id BETWEEN 1 AND 15000
 *
 *
 */
@Mapper
@Component("zuowenMapper")
public interface ZuowenMapper {

    @Select("<script>SELECT id, title, count, grade, type FROM zuowen WHERE 1=1 <if test=\"grade!=null and grade!=''\">AND grade = #{grade}</if> <if test=\"type!=null and type!=''\">and type=#{type}</if></script>")
    List<Zuowen> getList(String grade, String type);

    @Select("SELECT id, title, grade, type FROM zuowen ORDER BY count DESC limit 10")
    List<Zuowen> getHot();

    @Select("SELECT title, grade, type, count, content FROM zuowen WHERE id = #{id}")
    Zuowen getRecord(Integer id);

    @Update("UPDATE zuowen SET count = IFNULL(count, 0) + 1 WHERE id = #{id}")
    int countPlus(Integer id);

    @Select("<script>SELECT id, title FROM zuowen WHERE id IN " +
            "<foreach collection=\"ids\" item=\"id\" separator=\",\" open=\"(\" close=\")\">" +
            "#{id}" +
            "</foreach></script>")
    List<Zuowen> getTuijian(@Param("ids")List<Integer> ids);


    @Select("SELECT * FROM zuowen WHERE content like '%�%'")
    List<Zuowen> getErrorContentCodeZuowen();


    @Update("UPDATE zuowen SET content = #{content} WHERE id = #{id}")
    int updateContent(@Param("id") int id, @Param("content") String content);


    @Select("SELECT * FROM zuowen WHERE title like '%�%'")
    List<Zuowen> getErrorTitleCodeZuowen();

    @Update("UPDATE zuowen SET title = #{title} WHERE id = #{id}")
    int updateTitle(@Param("id") int id, @Param("title") String title);
}
